package maingoal;

import istar.agent.IstarJadeAgent;
import istar.impl.AbstractMainGoal;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.OneShotBehaviour;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class CitacoesObrasObtidas extends AbstractMainGoal {
	private boolean log = true;
	private boolean deliberateCondition = false;
	private String requestPath=null, resultPath=null;
	private String url, htmlCode="";
	
	public CitacoesObrasObtidas(IstarJadeAgent owner) {
		super("CitacoesObrasObtidas", owner);
	}

	public CitacoesObrasObtidas() {
		super("CitacoesObrasObtidas");
	}

	@Override
	public boolean deliberateCondition() {
		return deliberateCondition;
	}

	@Override
	public Behaviour getMainBehaviour() {
		return new ObterCitacoes(this);
	}
	
	private class ObterCitacoes extends OneShotBehaviour{
		AbstractMainGoal mainGoal;
		private String configurationPath;

		public ObterCitacoes(AbstractMainGoal _mainGoal){
			super();
			this.mainGoal = _mainGoal;
		}
		
		@Override
		public void action() {
			mainGoal.clear();
			this.configurationPath = ((IstarJadeAgent)myAgent).configurationPath;
            try {
            	BufferedReader in = new BufferedReader(new FileReader(configurationPath));
			    String str;
			    while ((str = in.readLine()) != null) {
			        if (str.startsWith("RequestPath=")){
			        	requestPath = str.substring(12);
			        }
			        if (str.startsWith("ResultPath=")){
			        	resultPath = str.substring(11);
			        }
			    }
			    in.close();
			    File requestDir = new File(requestPath);
			    File resultDir = new File(resultPath);
			    File workfile = null;

			    for ( int i = 0; i < requestDir.listFiles().length ; i++ ){
			    	File request = requestDir.listFiles()[i];
			    	workfile = request;
			    	for (int j=0; j< resultDir.listFiles().length; j++){
			    		File result  = resultDir.listFiles()[j];
			    		if (request.getName().equals(result.getName()))
			    			workfile = null;
			    	}
			    	if (workfile!=null){
			    		System.out.println("Executing file: " + workfile.getAbsolutePath());
			    		break;
			    	}
			    }
			    
            	if (workfile!=null){
            		String ticket = ((IstarJadeAgent)myAgent).getNextTicket();
        			mainGoal.setExecutionNumber(ticket);
    			    ((IstarJadeAgent)myAgent).setKnowledgeObject(mainGoal.getExecutionNumber(), "file_name", workfile.getName());
                	FileReader reader = new FileReader(workfile);
                	BufferedReader bReader = new BufferedReader(reader);
                	File output; 
                	output = new File(resultDir.getName() + workfile.getName());
    				output.createNewFile();

    				String linhaPagina = bReader.readLine();
    				while (linhaPagina!=null){
    					htmlCode += linhaPagina;
    					linhaPagina = bReader.readLine();
    				}
                	bReader.close();
                	boolean success = workfile.delete(); 
                	System.out.println("htmlCode: "+ htmlCode);

                	mainGoal.getContent().add(htmlCode);
        			((IstarJadeAgent)myAgent).setKnowledgeObject(mainGoal.getExecutionNumber(), "htmlCode", htmlCode);
        			
        			log("Starting deliberation. Ticket="+ mainGoal.getExecutionNumber());
        			log("url="+url);
                   	deliberateCondition = true;
            	}
            	else
            		deliberateCondition = false;
            } 
            catch(Exception e) {
            	e.printStackTrace();
            	deliberateCondition = false;
            }
		}

		@Override
		public int onEnd() {
			if (deliberateCondition){
				return 1;
			}
			else
				return 0;
		}
		
		public void log(String _phrase){
			((IstarJadeAgent)myAgent).log( _phrase);
		}
	}
	
}
 